/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import model.pojo.Aluno;

/**
 *
 * @author leonidia
 */
public class DaoBase implements DaoBaseInterface {

    /**
     *
     */
    protected static EntityManagerFactory factory = null;

    /**
     *Cria conexao com o banco de dados
     */
    public DaoBase() {
        factory = Persistence.createEntityManagerFactory("Filipe_Pedro_Erik_Julio");
    }

    /**
     *Retorna a conexao com o banco de dados
     * @return
     */
    @Override
    public EntityManager returnConection() {
        return factory.createEntityManager();
    }
    /**
     *Retorna o factory do banco de dados.
     * @return
     */
    @Override
    public EntityManagerFactory returnFactory() {
        return factory;
    }
/*
    @Override
    public void add(Object object) {
        EntityManager manager = returnConection();
        manager.getTransaction().begin();
        manager.persist(object);
        manager.getTransaction().commit();
        manager.close();
    }

    @Override
    public void remover(Object object) {
        EntityManager manager = returnConection();
        manager.remove(object);
    }

    @Override
    public Object buscarAluno(Object object, String cpf) {
        EntityManager manager = returnConection();
        manager.getTransaction().begin();
        Object resultado = manager.find(Aluno.class, cpf);
        manager.getTransaction().commit();
        return resultado;
    }

    @Override
    public void find() {
        try {
            EntityManager manager = returnConection();
            Query query = manager.createQuery("from Aluno c where c.nome = ’Jose’");
            Aluno c = (Aluno) query.getSingleResult();
            System.out.println(c.getCpf());
        } catch (EntityNotFoundException | NonUniqueResultException notFound) {
        }
    }
    @Override
    public List findCpfMatricula(Aluno aluno){
        EntityManager manager = returnConection();
        Query query = manager.createQuery("from Aluno aluno "+ "where aluno.cpf = :cpf and aluno.matricula = :matricula");
        query.setParameter("cpf",aluno.getCpf());
        query.setParameter("matricula",aluno.getMatricula());
        return query.getResultList();
    }
    @Override
    public void teste(){
        Aluno aluno = new Aluno("123","pedro","11541115635");
        List listaResultado = findCpfMatricula(aluno);
        for(Object object: listaResultado){
             Aluno alu = (Aluno)object;
             System.out.println(alu.getNome());
        }
    }

    @Override
    public void alterar(Object object) {
    }*/
}
